package com.portal.controller;

import com.portal.enums.RegisterResult;
import com.portal.model.dto.UserRegisterParam;
import com.portal.service.UserService;
import com.test.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@Api(value = "UserController",description = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public CommonResult<Integer> register(@ApiParam("注册对象")@RequestBody UserRegisterParam userRegisterParam){
        RegisterResult register = userService.register(userRegisterParam);
        if(register == RegisterResult.REG_SUCCESS){
            return CommonResult.success(1,register.getMessage());
        }
        return CommonResult.failed(register.getMessage());
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public CommonResult<Map<String,String>>login(@ApiParam("用户名")@RequestParam("userName")String userName,
                                                 @ApiParam("密码")@RequestParam("password")String password){
        System.out.println(userName);
        System.out.println(password);
        Map<String, String> login = userService.login(userName, password);
        if(!CollectionUtils.isEmpty(login)){
            return CommonResult.success(login,"登录成功");
        }
        return CommonResult.failed("登录失败");
    }

}
